package com.esunny.resourcevisualization.controller;

import com.esunny.resourcevisualization.domain.request.ProcessDealDto;
import com.esunny.resourcevisualization.domain.response.ReturnStructure;
import com.esunny.resourcevisualization.service.ProcessService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @version 1.0.0.0
 * @ClassName ProcessController
 * @Description 流程控制接口
 * @Author baochencong
 * @Date 2020/11/27 15:14
 */
@Controller
@RequestMapping("/process")
public class ProcessController {

    @Resource
    private ProcessService processService;

    @ResponseBody
    @PostMapping("/submitApply")
    public ReturnStructure submitApply(@RequestParam("applyId") int applyId) {
        if (applyId == 0) {
            return ReturnStructure.NOT_FOUND();
        }
        return processService.submitApply(applyId);
    }

    @ResponseBody
    @PostMapping("/cancelApply")
    public ReturnStructure cancelApply(@RequestParam("applyId") int applyId) {
        if (applyId == 0) {
            return ReturnStructure.NOT_FOUND();
        }
        return processService.cancelApply(applyId);
    }

    @ResponseBody
    @PostMapping("/auditApply")
    public ReturnStructure auditApply(@RequestBody ProcessDealDto processDealDto) {
        if (processDealDto == null || processDealDto.getApplyId() == 0) {
            return ReturnStructure.NOT_FOUND();
        }
        return processService.auditApply(processDealDto.getApplyId(), processDealDto.isAgree(), processDealDto.getRemark());
    }

    @ResponseBody
    @PostMapping("/assignResource")
    public ReturnStructure assignResource(@RequestBody ProcessDealDto processDealDto) {
        if (processDealDto == null || processDealDto.getApplyId() == 0) {
            return ReturnStructure.NOT_FOUND();
        }
        return processService.assignResource(processDealDto);
    }

    /**
     * 分配资源阶段驳回申请
     *
     * @param processDealDto 驳回信息
     * @return 驳回结果
     */
    @ResponseBody
    @PostMapping("/rebackApply")
    public ReturnStructure rebackApply(@RequestBody ProcessDealDto processDealDto) {
        if (processDealDto == null || processDealDto.getApplyId() == 0) {
            return ReturnStructure.NOT_FOUND();
        }
        return processService.rebackApply(processDealDto);
    }

    @ResponseBody
    @PostMapping("/returnResource")
    public ReturnStructure returnResource(@RequestParam("applyId") int applyId) {
        if (applyId == 0) {
            return ReturnStructure.NOT_FOUND();
        }
        return processService.returnResource(applyId);
    }

    @ResponseBody
    @PostMapping("/freeResource")
    public ReturnStructure freeResource(@RequestParam("applyId") int applyId) {
        if (applyId == 0) {
            return ReturnStructure.NOT_FOUND();
        }
        return processService.freeResource(applyId);
    }

    @ResponseBody
    @PostMapping("/advanceFreeResource")
    public ReturnStructure advanceFreeResource(@RequestBody ProcessDealDto processDealDto) {
        if (processDealDto == null || processDealDto.getApplyId() == 0) {
            return ReturnStructure.NOT_FOUND();
        }
        return processService.advanceFreeResource(processDealDto.getApplyId(), processDealDto.getRemark());
    }

    @ResponseBody
    @GetMapping("/viewProcessImage/{processInstanceId}")
    public ReturnStructure viewProcessImage(@PathVariable("processInstanceId") String processInstanceId, HttpServletResponse response) {
        return processService.viewProcessImage(processInstanceId, response);
    }

}
